package com.qing.common.adpter;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.qing.common.base.BaseAdapter;
import com.qing.common.base.BaseViewHolder;
import com.qing.common.databinding.ItemImageLayoutBinding;
import com.qing.common.util.ConvertUtils;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

public class PhotoPreviewAdapter extends BaseAdapter<PhotoPreviewAdapter.Data> {

    public PhotoPreviewAdapter(List<Data> list) {
        super(list, EMPTY_TYPE_NONE, false, false);
    }

    @Override
    protected BaseViewHolder<?, ?> createHeaderViewHolder(@NonNull ViewGroup parent) {
        return null;
    }

    @Override
    protected BaseViewHolder<?, ?> createContentViewHolder(@NonNull ViewGroup parent) {
        return new ViewHolder(ItemImageLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    protected BaseViewHolder<?, ?> createFooterViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    protected void bindContentViewHolder(@NonNull BaseViewHolder<?, ?> holder, Data item) {
        ConvertUtils.INSTANCE.applyConvert(holder, ViewHolder.class, vb -> {
            if (getDataSize() == 1) {
                RecyclerView recyclerView = getRecyclerView();
                if (recyclerView != null) {
                    recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
                }
            }
            vb.bind(item);
            return null;
        });
    }

    public static class ViewHolder extends BaseViewHolder<Data, ItemImageLayoutBinding> {
        public ViewHolder(@NonNull ItemImageLayoutBinding binding) {
            super(binding);
        }

        @Override
        public void bind(@NonNull Data data) {
            if (data.getUrl() != null) {
                Glide.with(getBinding().imageView)
                        .asDrawable()
                        .load(Uri.parse(data.getUrl()))
                        .into(getBinding().imageView);
            } else {
                Glide.with(getBinding().imageView)
                        .asDrawable()
                        .load(data.getResId())
                        .into(getBinding().imageView);
            }
            getBinding().imageView.setTransitionName(data.getSharedElementName());
        }
    }

    @lombok.Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Data implements Parcelable {
        public static final Creator<Data> CREATOR = new Creator<Data>() {
            @Override
            public Data createFromParcel(Parcel in) {
                return new Data(in);
            }

            @Override
            public Data[] newArray(int size) {
                return new Data[size];
            }
        };
        private String url;
        private int resId;
        private String sharedElementName;

        protected Data(Parcel in) {
            url = in.readString();
            resId = in.readInt();
            sharedElementName = in.readString();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeString(url);
            dest.writeInt(resId);
            dest.writeString(sharedElementName);
        }
    }
}
